package com.xunyin.utils;

public class BCCUtil {
    /**
     * BBC校验位
     *
     * @param hexString
     * @author jthong
     * @Version 1.0.0
     * @Date 2019/12/26 16:18
     */
    public static String getBCC(String hexString) {
        byte[] data = hexStringToBytes(hexString);
        String ret = "";
        byte BCC[] = new byte[1];
        for (int i = 0; i < data.length; i++) {
            BCC[0] = (byte) (BCC[0] ^ data[i]);
        }
        String hex = Integer.toHexString(BCC[0] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        ret += hex.toUpperCase();
        return ret;
    }

    /**
     * Convert char to byte
     *
     * @param c char
     * @return byte
     */
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }

    private static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || hexString.equals("")) {
            return null;
        }
        hexString = hexString.replaceAll(" ", "");
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
        }
        return d;
    }

    public static void main(String args[]) {
        int base = 12;
        int index = 0;
        for (int i = 0; i < base; i++) {
            for (int j = 0; j < base; j++) {
                for (int k = 0; k < base; k++) {
                    index++;
                    System.out.println(index);
                }
            }
        }
    }
}
